<?php


include_once XOOPS_ROOT_PATH."/class/xoopsobject.php";

class neoobjectitempropcat  extends XoopsObject{

	function neoobjectitempropcat (){
	$this->XoopsObject();
	#$this->initVar('dohtml', XOBJ_DTYPE_INT, '1');	
	$this->initVar('id', XOBJ_DTYPE_INT, 0, false);								
	$this->initVar('title', XOBJ_DTYPE_TXTBOX, '', false);	
	$this->initVar('description', XOBJ_DTYPE_TXTBOX, '', false);			
	$this->initVar('pid', XOBJ_DTYPE_INT, 0, false);					
	 						
	}
	
}


class neoobjectitempropcatHandler extends XoopsObjectHandler{
	var $table='neoobjectitempropcat';
    function &create($isNew = true)
    {
        $item = new neoobjectitempropcat();
        if ($isNew) {
            $item->setNew();
        }
        return $item;
    }
	
	
    function &get($id)
    {
        $id = intval($id);
        if ($id > 0) {
		
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table).' WHERE id='.$id;
            if (!$result = $this->db->query($sql)) {
                return false;
            }
            $numrows = $this->db->getRowsNum($result);
            if ($numrows == 1) {
                $item = new neoobjectitempropcat();
                $item->assignVars($this->db->fetchArray($result));
                return $item;
            }
        }
        return false;
    }
	
	 function insert(&$item)
    {
        if (get_class($item) != 'neoobjectitempropcat') {
            return false;
        }

/*        if (!$item->isDirty()) {
            return true;
        }
*/
         if (!$item->cleanVars()) {
            return false;
        }
        foreach ($item->cleanVars as $k => $v) {
            ${$k} = $v;
        }
        if ($item->isNew()) {
            $item_id = $this->db->genId('neoobjectitempropcat_seq');
            $sql = sprintf("INSERT INTO %s (id,title,description,pid) 
					VALUES (0, %s, %s, %u)",
					 $this->db->prefix($this->table),
					  $this->db->quoteString($title),
					  $this->db->quoteString($description),
					  $pid);
					  
		#	echo $sql;         	
		    if (!$result = $this->db->query($sql)) {
				echo $this->db->error();
                return false;
            }
            if (empty($id)) {
                $id = $this->db->getInsertId();
            }
            $item->assignVar('id', $id);
        } else {
			#echo $sql;
            $sql = sprintf("UPDATE  %s SET title=%s,description=%s,pid=%u WHERE id=".$id,
					 $this->db->prefix($this->table),
					  $this->db->quoteString($title),
					  $this->db->quoteString($description),
					  $pid);

            if (!$result = $this->db->query($sql)) {
				echo $this->db->error();
                return false;
            }
        }
        return true;
    }

#type 0 arr of objects objects
#	1 resource
#	2 arr of arr
    function &getObjects($criteria = null, $id_as_key = false, $is_res=false)
    {
        $ret = array();
        $limit = $start = 0;
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table)." ";
     
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
            
			$sql .= $criteria->renderWhere();
			$sort =  $criteria->getSort();
            $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
            $limit = $criteria->getLimit();
            $start = $criteria->getStart();
        }
	#echo $sql;
        $result = $this->db->query($sql, $limit, $start);
        if (!$result) {
            return $ret;
        }
		if ($is_res) return $result;
        while ($myrow = $this->db->fetchArray($result)) {
            $item = new neoobjectitempropcat();
            $item->assignVars($myrow);
      
		    if (!$id_as_key) {
                $ret[] =& $item;
            } else {
                $ret[$myrow['id']] =& $item;
            }
            unset($item);
		}		 
        return $ret;
    }

    /**
     * Count some images
     * 
     * @param   object  $criteria   {@link CriteriaElement} 
     * @return  int
     **/
    function getCount($criteria = null)
    {
        $limit = $start = 0;
            $sql = 'SELECT * FROM '.$this->db->prefix($this->table)." ";
     
        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
            
			$sql .= $criteria->renderWhere();

        }


        if (!$result =& $this->db->query($sql)) {
            return 0;
        }
        $count = $this->db->getRowsNum($result);
        return $count;
    }
function delete($item)
    {

	 if (get_class($item) != 'neoobjectitempropcat') {
            return false;
        }

/*        if (!$item->isDirty()) {
            return true;
        }
*/
         if (!$item->cleanVars()) {
            return false;
        }
        foreach ($item->cleanVars as $k => $v) {
            ${$k} = $v;
        }
				
     
        if ($id > 0) {
		
            $sql = 'DELETE FROM '.$this->db->prefix($this->table).' WHERE id='.$id;
        #   echo $sql;
			if (!$result = $this->db->queryF($sql)) {
				echo $this->db->Error();
                return false;
            }
			
			
        }   
        return true;
    	}


}

?>